package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MyFileAdapter;
import flc.ast.bean.MyFileBean;
import flc.ast.databinding.ActivityMyFileBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaInfo;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class MyFileActivity extends BaseAc<ActivityMyFileBinding> {

    private MyFileAdapter fileAdapter;
    private List<MyFileBean> listShow = new ArrayList<>();
    private List<String> listSel = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_my_file;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMyFileBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvAddFile.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new MyFileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvAddFile://添加文件
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_file));
                    return;
                }
                for (String path : listSel) {
                    String filePath = FileUtil.generateFilePath(Content.myPrivacy + "/" + FolderActivity.folderName, "." + FileUtils.getFileExtension(path));
                    FileUtils.copy(path, filePath);
                }
                ToastUtils.showShort(getString(R.string.add_file_suc));
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (fileAdapter.getItem(position).isSelected()) {
            fileAdapter.getItem(position).setSelected(false);
            listSel.remove(fileAdapter.getItem(position).getPath());
        } else {
            fileAdapter.getItem(position).setSelected(true);
            listSel.add(fileAdapter.getItem(position).getPath());
        }
        fileAdapter.notifyItemChanged(position);
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取文档数据
    private void getData() {
        //设置获取的数据类型 只获取pdf
        List<String> type = new ArrayList<>();
        type.add("application/msword");
        type.add("application/vnd.ms-excel");
        type.add("application/vnd.ms-powerpoint");
        type.add("application/pdf");
        type.add("text/plain");
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<MediaInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MediaInfo>> emitter) {
                List<MediaInfo> infoList = MediaLoader.loadDoc(type);
                emitter.onNext(infoList);
            }

            @Override
            public void accept(List<MediaInfo> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    for (MediaInfo bean : selectMediaEntities) {
                        listShow.add(new MyFileBean(bean.getPath()));
                    }
                }
                if (listShow.size() > 0) {
                    fileAdapter.setList(listShow);
                    mDataBinding.rvFileList.setVisibility(View.VISIBLE);
                    mDataBinding.tvFileNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvFileList.setVisibility(View.GONE);
                    mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}